package org.example.ch05.service;

import lombok.RequiredArgsConstructor;
import org.example.ch05.config.RabbitConfig;
import org.example.ch05.dto.Order;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @ClassName OrderProducerService
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/11 15:15
 * @Version 1.0
 */
@Service
@RequiredArgsConstructor
public class OrderProducerService {
    /**
     * 注入 rabbitTemplate 来投递信息
    */
    private final RabbitTemplate rabbitTemplate;

    /**
     * @author: kx
     * @date: 2025/10/11 15:17
     * @param message : 消息内容
     * @description: 投递消息
    */
    public void publish(String message) {
        // 创建消息唯一的标识
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(UUID.randomUUID().toString());

        // 消息投递
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, message, correlationData);
    }

    public void publish(Order order) {
        //创建消息的唯一ID
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(UUID.randomUUID().toString());
        //消息的投递
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME,
                RabbitConfig.ROUTING_KEY,
                order,
                correlationData);
    }
}
